<?php
if($_POST)
{
	$domian = 'http://'.$_POST['sele-text-host'];
	if(!$_POST['mode'] || !$_POST['contect'] || !$_POST['cates'])
	{
		header("Content-type: text/html; charset=utf-8");
		die('ERRO:无提交数据！');
	}

	$pattern = preg_replace(array('/_0_/','/_1_/'),array('(?:\s?[^\s]*\s?)','([^"]+)'),str_replace('/','\/',$_POST['mode']));	
	$contect = doString($_POST['contect']);
	$contect = strip_tags($contect, '<a>,<img>');

	preg_match_all("/$pattern/",$contect, $out, PREG_PATTERN_ORDER);
	
	$num = count($out);
	if(!$out[0])
	{
		die('ERRO:没有匹配到数据！');
	}

	foreach($out[0] as $key => $val)
	{
		for($i = 1;$i < $num;$i++)
		{
			$hz = end(explode('.',basename($out[$i][$key])));
			switch($hz)
			{
				case 'jpg':
				case 'gif':
				case 'png':
					$data[$key]['img'] = $out[$i][$key];
					break;
				case 'shtml':
				case 'html':
				case 'php':
				case 'htm':
					$data[$key]['link'] = $out[$i][$key];
					if(strpos($val['url'],'http') === false)
					{
						$data[$key]['link'] = $domian.$data[$key]['link'];
					}					
					break;
				default:
					$data[$key]['name'] = $out[$i][$key];
					break;
			}
		}
	}
	
	foreach($data as $val)
	{
		$_->source->_insert(array('name'=>$val['name'],'link'=>$val['link'],'img'=>$val['img'],'cates'=>intval($_POST['cates']),'step'=>1));
	}
}	
	$data = $_->source->_select('*',array('DESC'=>'id','LIMIT'=>'0,40'));
	$catename = array('1'=>'php','2'=>'css','3'=>'javascript','4'=>'templates');
	$status = array('0'=>'<font color=red>已上架</font>','1'=>'<font color=gray>待更新</font>','2'=>'<font color=green>已更新</font>');	
	$smarty->_assign(array('data','catename','status'));
	$smarty->_display();

	function doString($string)
	{
		$string = str_replace(array(PHP_EOL,'\"'), array('','"'),$string);
		return preg_replace('/>\s+</', '><', $string);
	}
?>